
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>6.2 缓冲区对象 · 游戏引擎浅入浅出</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="captainchen">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-lightbox/css/lightbox.min.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../7. Mesh和材质/7. Mesh和材质.html" />
    
    
    <link rel="prev" href="6.1 顶点索引.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">目录</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../前言.html">
            
                <a href="../前言.html">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../1. 游戏引擎框架介绍/1. 游戏引擎框架介绍.html">
            
                <a href="../1. 游戏引擎框架介绍/1. 游戏引擎框架介绍.html">
            
                    
                    1. 游戏引擎框架介绍
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.2.1" data-path="../1. 游戏引擎框架介绍/1.1 暗中观察unity.html">
            
                <a href="../1. 游戏引擎框架介绍/1.1 暗中观察unity.html">
            
                    
                    1.1 暗中观察unity
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.2" data-path="../1. 游戏引擎框架介绍/1.2 游戏引擎组成.html">
            
                <a href="../1. 游戏引擎框架介绍/1.2 游戏引擎组成.html">
            
                    
                    1.2 游戏引擎组成
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../2. Opengl开发环境搭建/2. Opengl开发环境搭建.html">
            
                <a href="../2. Opengl开发环境搭建/2. Opengl开发环境搭建.html">
            
                    
                    2. Opengl开发环境搭建
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.3.1" data-path="../2. Opengl开发环境搭建/2.1 Opengl到底是什么.html">
            
                <a href="../2. Opengl开发环境搭建/2.1 Opengl到底是什么.html">
            
                    
                    2.1 Opengl到底是什么
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.2" data-path="../2. Opengl开发环境搭建/2.2 搭建Opengl开发环境.html">
            
                <a href="../2. Opengl开发环境搭建/2.2 搭建Opengl开发环境.html">
            
                    
                    2.2 搭建Opengl开发环境
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../3. 绘制简单图形/3. 绘制简单图形.html">
            
                <a href="../3. 绘制简单图形/3. 绘制简单图形.html">
            
                    
                    3. 绘制简单图形
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.4.1" data-path="../3. 绘制简单图形/3.1 画个三角形.html">
            
                <a href="../3. 绘制简单图形/3.1 画个三角形.html">
            
                    
                    3.1 画个三角形
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.2" data-path="../3. 绘制简单图形/3.2 画个正方形.html">
            
                <a href="../3. 绘制简单图形/3.2 画个正方形.html">
            
                    
                    3.2 画个正方形
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.3" data-path="../3. 绘制简单图形/3.3 画个立方体.html">
            
                <a href="../3. 绘制简单图形/3.3 画个立方体.html">
            
                    
                    3.3 画个立方体
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../4. 着色器/4. 着色器.html">
            
                <a href="../4. 着色器/4. 着色器.html">
            
                    
                    4. 着色器
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.5.1" data-path="../4. 着色器/4.1 着色器是什么.html">
            
                <a href="../4. 着色器/4.1 着色器是什么.html">
            
                    
                    4.1 着色器是什么
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.2" data-path="../4. 着色器/4.2 Unity Shader和OpenGL Shader.html">
            
                <a href="../4. 着色器/4.2 Unity Shader和OpenGL Shader.html">
            
                    
                    4.2 Unity Shader和OpenGL Shader
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.3" data-path="../4. 着色器/4.3 顶点着色器.html">
            
                <a href="../4. 着色器/4.3 顶点着色器.html">
            
                    
                    4.3 顶点着色器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5.4" data-path="../4. 着色器/4.4 片段着色器.html">
            
                <a href="../4. 着色器/4.4 片段着色器.html">
            
                    
                    4.4 片段着色器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../5. 绘制贴图/5. 绘制贴图.html">
            
                <a href="../5. 绘制贴图/5. 绘制贴图.html">
            
                    
                    5. 绘制贴图
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.6.1" data-path="../5. 绘制贴图/5.1 颜色和贴图.html">
            
                <a href="../5. 绘制贴图/5.1 颜色和贴图.html">
            
                    
                    5.1 颜色和贴图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.2" data-path="../5. 绘制贴图/5.2 贴图文件介绍.html">
            
                <a href="../5. 绘制贴图/5.2 贴图文件介绍.html">
            
                    
                    5.2 贴图文件介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.3" data-path="../5. 绘制贴图/5.3 CPU与GPU的通信方式.html">
            
                <a href="../5. 绘制贴图/5.3 CPU与GPU的通信方式.html">
            
                    
                    5.3 CPU与GPU的通信方式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.4" data-path="../5. 绘制贴图/5.4 使用stb_image解析图片.html">
            
                <a href="../5. 绘制贴图/5.4 使用stb_image解析图片.html">
            
                    
                    5.4 使用stb_image解析图片
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.5" data-path="../5. 绘制贴图/5.5 绘制带贴图的立方体盒子.html">
            
                <a href="../5. 绘制贴图/5.5 绘制带贴图的立方体盒子.html">
            
                    
                    5.5 绘制带贴图的立方体盒子
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.6" data-path="../5. 绘制贴图/5.6 压缩纹理.html">
            
                <a href="../5. 绘制贴图/5.6 压缩纹理.html">
            
                    
                    5.6 压缩纹理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.7" data-path="../5. 绘制贴图/5.7 图片压缩工具.html">
            
                <a href="../5. 绘制贴图/5.7 图片压缩工具.html">
            
                    
                    5.7 图片压缩工具
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6.8" data-path="../5. 绘制贴图/5.8 使用压缩纹理.html">
            
                <a href="../5. 绘制贴图/5.8 使用压缩纹理.html">
            
                    
                    5.8 使用压缩纹理
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="6. 缓冲区对象.html">
            
                <a href="6. 缓冲区对象.html">
            
                    
                    6. 缓冲区对象
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.7.1" data-path="6.1 顶点索引.html">
            
                <a href="6.1 顶点索引.html">
            
                    
                    6.1 顶点索引
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.7.2" data-path="6.2 缓冲区对象.html">
            
                <a href="6.2 缓冲区对象.html">
            
                    
                    6.2 缓冲区对象
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../7. Mesh和材质/7. Mesh和材质.html">
            
                <a href="../7. Mesh和材质/7. Mesh和材质.html">
            
                    
                    7. Mesh和材质
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.8.1" data-path="../7. Mesh和材质/7.1 导出Mesh文件.html">
            
                <a href="../7. Mesh和材质/7.1 导出Mesh文件.html">
            
                    
                    7.1 导出Mesh文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8.2" data-path="../7. Mesh和材质/7.2 使用Mesh文件.html">
            
                <a href="../7. Mesh和材质/7.2 使用Mesh文件.html">
            
                    
                    7.2 使用Mesh文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8.3" data-path="../7. Mesh和材质/7.3 Shader文件创建与使用.html">
            
                <a href="../7. Mesh和材质/7.3 Shader文件创建与使用.html">
            
                    
                    7.3 Shader文件创建与使用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >6.2 缓冲区对象</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <html><head></head><body><div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><ul><li><span class="title-icon "></span><a href="#62-&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;"><b></b>6.2 &#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;</a></li><ul><li><span class="title-icon "></span><a href="#1-&#x521B;&#x5EFA;vbo&#x548C;ebo&#xFF0C;&#x5E76;&#x4E0A;&#x4F20;&#x6570;&#x636E;"><b></b>1. &#x521B;&#x5EFA;VBO&#x548C;EBO&#xFF0C;&#x5E76;&#x4E0A;&#x4F20;&#x6570;&#x636E;</a></li><li><span class="title-icon "></span><a href="#2-&#x5C06;shader&#x53D8;&#x91CF;&#x548C;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x5173;&#x8054;"><b></b>2. &#x5C06;Shader&#x53D8;&#x91CF;&#x548C;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x5173;&#x8054;</a></li><li><span class="title-icon "></span><a href="#3-&#x7ED8;&#x5236;"><b></b>3. &#x7ED8;&#x5236;</a></li></ul></ul></ul></div><h2 id="62-&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;"><a name="62-&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;" class="anchor-navigation-ex-anchor" href="#62-&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;"><i class="fa fa-link" aria-hidden="true"></i></a>6.2 &#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;</h2>
<pre><code>CLion&#x9879;&#x76EE;&#x6587;&#x4EF6;&#x4F4D;&#x4E8E; samples\vertex_index_and_buffer\vbo
</code></pre><p>&#x4E0A;&#x4E00;&#x8282;&#x4F7F;&#x7528; <code>glDrawElements</code> &#x901A;&#x8FC7;&#x9876;&#x70B9;&#x7D22;&#x5F15;&#x8FDB;&#x884C;&#x7ED8;&#x5236;&#xFF0C;&#x5927;&#x5E45;&#x5EA6;&#x51CF;&#x5C11;&#x4E86;&#x4E0A;&#x4F20;&#x5230;GPU&#x7684;&#x6570;&#x636E;&#xFF0C;&#x4F46;&#x662F;&#x4ECD;&#x7136;&#x662F;&#x6BCF;&#x4E00;&#x5E27;&#x90FD;&#x4E0A;&#x4F20;&#x4E00;&#x6B21;&#x3002;
&#x662F;&#x5426;&#x53EF;&#x4EE5;&#x5C06;&#x6570;&#x636E;&#x7F13;&#x5B58;&#x5728;GPU&#xFF0C;&#x8FD9;&#x6837;&#x53EA;&#x9700;&#x8981;&#x4E0A;&#x4F20;&#x4E00;&#x6B21;&#x5373;&#x53EF;&#xFF1F;</p>
<p>OpenGL&#x5F15;&#x5165;&#x4E86;<code>Buffer Object</code>&#xFF0C;&#x5373;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;&#x3002;</p>
<p>&#x5728;&#x4E4B;&#x524D;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x9876;&#x70B9;&#x6570;&#x636E;&#x548C;&#x9876;&#x70B9;&#x7D22;&#x5F15;&#x7684;&#x6D41;&#x5411;&#x5982;&#x4E0B;&#x56FE;&#xFF1A;
<a href="../../imgs/vbo_vertex_index/vbo/upload_vertex_every_draw.png" data-lightbox="34cd8e13-1b23-4104-a2e3-8e3b4dc3f36c" data-title=""><img src="../../imgs/vbo_vertex_index/vbo/upload_vertex_every_draw.png" alt=""></a></p>
<p>&#x4ECE;&#x786C;&#x76D8;&#x8BFB;&#x53D6;&#x6A21;&#x578B;&#x6570;&#x636E;&#xFF0C;&#x62F7;&#x8D1D;&#x5230;&#x5185;&#x5B58;&#x4E2D;&#xFF0C;&#x7136;&#x540E;&#x6BCF;&#x6B21;&#x7ED8;&#x5236;&#x7684;&#x65F6;&#x5019;&#x4E0A;&#x4F20;&#x5230;&#x663E;&#x5361;&#x3002;
&#x5E73;&#x65F6;&#x73A9;&#x6E38;&#x620F;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6BCF;&#x8FC7;&#x4E00;&#x4E2A;&#x573A;&#x666F;&#xFF0C;&#x90FD;&#x4F1A;&#x6253;&#x5F00;&#x4E00;&#x4E2A; Loading &#x754C;&#x9762;&#xFF0C;&#x5728;Loading &#x754C;&#x9762;&#x51FA;&#x73B0;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5C31;&#x662F;&#x4ECE;&#x786C;&#x76D8;&#x4E2D;&#x8BFB;&#x53D6;&#x9876;&#x70B9;&#x6570;&#x636E;&#x5230;&#x8BBE;&#x5907;&#x5185;&#x5B58;&#x4E2D;&#x3002;
&#x5F53;&#x8981;&#x6E32;&#x67D3;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5C31;&#x76F4;&#x63A5;&#x4ECE;&#x5185;&#x5B58;&#x4E2D;&#x628A;&#x9876;&#x70B9;&#x6570;&#x636E;&#x8BFB;&#x53D6;&#x51FA;&#x6765;&#xFF0C;&#x7136;&#x540E;&#x4E0A;&#x4F20;&#x5230;&#x663E;&#x5361;&#x7684;&#x663E;&#x5B58;&#x4E2D;&#x3002;</p>
<p>&#x4F7F;&#x7528;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;&#x4E4B;&#x540E;&#xFF0C;&#x5982;&#x4E0B;&#x56FE;&#xFF1A;
<a href="../../imgs/vbo_vertex_index/vbo/draw_with_vbo.png" data-lightbox="9c6a454e-badb-4ecd-8819-a3f6da865655" data-title=""><img src="../../imgs/vbo_vertex_index/vbo/draw_with_vbo.png" alt=""></a></p>
<p>&#x9876;&#x70B9;&#x6570;&#x636E;&#x4E0A;&#x4F20;&#x5230;GPU&#x4E4B;&#x540E;&#xFF0C;&#x5C31;&#x7F13;&#x5B58;&#x8D77;&#x6765;&#xFF0C;&#x540E;&#x7EED;&#x6E32;&#x67D3;&#x76F4;&#x63A5;&#x4ECE;&#x663E;&#x5B58;&#x83B7;&#x53D6;&#x3002;</p>
<p>&#x4F7F;&#x7528;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x7ED8;&#x5236;&#x5206;&#x4E3A;&#x4EE5;&#x4E0B;&#x6B65;&#x9AA4;&#xFF1A;</p>
<ol>
<li>&#x521B;&#x5EFA;VBO(&#x9876;&#x70B9;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;) &#x548C; EBO(&#x7D22;&#x5F15;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;)&#xFF0C;&#x5E76;&#x4E0A;&#x4F20;&#x6570;&#x636E;</li>
<li>&#x5C06;Shader&#x53D8;&#x91CF;&#x548C;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x5173;&#x8054;</li>
<li>&#x4F7F;&#x7528;EBO&#x7ED8;&#x5236;</li>
</ol>
<p>&#x4E0B;&#x9762;&#x4E00;&#x4E00;&#x6765;&#x770B;&#x3002;</p>
<h3 id="1-&#x521B;&#x5EFA;vbo&#x548C;ebo&#xFF0C;&#x5E76;&#x4E0A;&#x4F20;&#x6570;&#x636E;"><a name="1-&#x521B;&#x5EFA;vbo&#x548C;ebo&#xFF0C;&#x5E76;&#x4E0A;&#x4F20;&#x6570;&#x636E;" class="anchor-navigation-ex-anchor" href="#1-&#x521B;&#x5EFA;vbo&#x548C;ebo&#xFF0C;&#x5E76;&#x4E0A;&#x4F20;&#x6570;&#x636E;"><i class="fa fa-link" aria-hidden="true"></i></a>1. &#x521B;&#x5EFA;VBO&#x548C;EBO&#xFF0C;&#x5E76;&#x4E0A;&#x4F20;&#x6570;&#x636E;</h3>
<pre><code class="lang-c++"><span class="hljs-comment">//main.cpp</span>

<span class="hljs-comment">//&#x521B;&#x5EFA;VBO&#x548C;EBO</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">GeneratorBufferObject</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-comment">//&#x5728;GPU&#x4E0A;&#x521B;&#x5EFA;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;</span>
    glGenBuffers(<span class="hljs-number">1</span>,&amp;kVBO);
    <span class="hljs-comment">//&#x5C06;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;&#x6307;&#x5B9A;&#x4E3A;&#x9876;&#x70B9;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;</span>
    glBindBuffer(GL_ARRAY_BUFFER, kVBO);
    <span class="hljs-comment">//&#x4E0A;&#x4F20;&#x9876;&#x70B9;&#x6570;&#x636E;&#x5230;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;</span>
    glBufferData(GL_ARRAY_BUFFER, kVertexRemoveDumplicateVector.size() * <span class="hljs-keyword">sizeof</span>(Vertex), &amp;kVertexRemoveDumplicateVector[<span class="hljs-number">0</span>], GL_STATIC_DRAW);

    <span class="hljs-comment">//&#x5728;GPU&#x4E0A;&#x521B;&#x5EFA;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;</span>
    glGenBuffers(<span class="hljs-number">1</span>,&amp;kEBO);
    <span class="hljs-comment">//&#x5C06;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;&#x6307;&#x5B9A;&#x4E3A;&#x9876;&#x70B9;&#x7D22;&#x5F15;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;</span>
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, kEBO);
    <span class="hljs-comment">//&#x4E0A;&#x4F20;&#x9876;&#x70B9;&#x7D22;&#x5F15;&#x6570;&#x636E;&#x5230;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;</span>
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, kVertexIndexVector.size() * <span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">short</span>), &amp;kVertexIndexVector[<span class="hljs-number">0</span>], GL_STATIC_DRAW);
}
</code></pre>
<p>&#x5728;GPU&#x4E0A;&#x521B;&#x5EFA;&#x5BF9;&#x8C61;&#xFF0C;&#x90FD;&#x662F;&#x5206;&#x4E09;&#x6B65;&#xFF0C;&#x8C03;&#x7528;3&#x4E2A;API&#xFF1A;</p>
<ol>
<li>glGenxxxx &#x5728;GPU&#x4E0A;&#x8FDB;&#x884C;&#x521B;&#x5EFA;xx&#x5BF9;&#x8C61;</li>
<li>glBindxxx &#x5C06;xx&#x5BF9;&#x8C61;&#x6307;&#x5B9A;&#x4E3A;&#x7C7B;&#x578B;</li>
<li>glxxxxxxx &#x4E0A;&#x4F20;&#x6570;&#x636E;</li>
</ol>
<p>&#x4E0E;&#x521B;&#x5EFA;&#x7EB9;&#x7406;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x5BF9;&#x6BD4;&#xFF1A;</p>
<table>
<tbody><tr bgcolor="AliceBlue"><td><b>&#x6B65;&#x9AA4;</b></td><td><b>&#x7EB9;&#x7406;</b></td><td><b></b></td><td><b>VBO</b></td><td><b></b></td></tr>
<tr><td>1</td><td>glGenTextures</td><td>&#x901A;&#x77E5;&#x663E;&#x5361;&#x521B;&#x5EFA;&#x7EB9;&#x7406;&#x5BF9;&#x8C61;</td><td>glGenBuffers</td><td>&#x5728;GPU&#x521B;&#x5EFA;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;</td></tr>
<tr><td>2</td><td>glBindTexture</td><td>&#x5C06;&#x7EB9;&#x7406;&#x7ED1;&#x5B9A;&#x5230;&#x7279;&#x5B9A;&#x7EB9;&#x7406;&#x76EE;&#x6807;</td><td>glBindBuffer</td><td>&#x5C06;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;&#x6307;&#x5B9A;&#x4E3A;&#x9876;&#x70B9;&#x7D22;&#x5F15;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;</td></tr>
<tr><td>3</td><td>glTexImage2D</td><td>&#x5C06;&#x56FE;&#x7247;rgb&#x6570;&#x636E;&#x4E0A;&#x4F20;&#x5230;GPU;</td><td>glBufferData</td><td>&#x4E0A;&#x4F20;&#x9876;&#x70B9;&#x7D22;&#x5F15;&#x6570;&#x636E;&#x5230;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;</td></tr>
</tbody></table>

<p>&#x7B80;&#x5355;&#x4ECB;&#x7ECD;&#x8FD9;3&#x4E2A;API&#xFF1A;</p>
<pre><code class="lang-c++"><span class="hljs-comment">/**
*@brief &#x5728;GPU&#x4E0A;&#x521B;&#x5EFA;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;
*@param n &#x521B;&#x5EFA;&#x4E2A;&#x6570;
*@param buffers &#x7F13;&#x51B2;&#x533A;&#x53E5;&#x67C4;
*/</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">glGenBuffers</span><span class="hljs-params">(GLsizei   n, GLuint  * buffers)</span></span>;
</code></pre>
<pre><code class="lang-c++"><span class="hljs-comment">/**
*@brief &#x5C06;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;&#x6307;&#x5B9A;&#x4E3A;&#x7279;&#x5B9A;&#x76EE;&#x6807;
*@param target &#x76EE;&#x6807;&#xFF0C;&#x5E38;&#x7528;GL_ARRAY_BUFFER(&#x9876;&#x70B9;&#x5C5E;&#x6027;&#x7F13;&#x51B2;&#x533A;) GL_ELEMENT_ARRAY_BUFFER(&#x9876;&#x70B9;&#x7D22;&#x5F15;&#x7F13;&#x51B2;&#x533A;)
*@param buffers &#x7F13;&#x51B2;&#x533A;&#x53E5;&#x67C4;
*/</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">glBindBuffer</span><span class="hljs-params">(GLenum   target, GLuint   buffer)</span></span>;
</code></pre>
<pre><code class="lang-c++"><span class="hljs-comment">/**
*@brief &#x5C06;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;&#x6307;&#x5B9A;&#x4E3A;&#x7279;&#x5B9A;&#x76EE;&#x6807;
*@param target &#x76EE;&#x6807;&#xFF0C;&#x5E38;&#x7528;GL_ARRAY_BUFFER(&#x9876;&#x70B9;&#x5C5E;&#x6027;&#x7F13;&#x51B2;&#x533A;) GL_ELEMENT_ARRAY_BUFFER(&#x9876;&#x70B9;&#x7D22;&#x5F15;&#x7F13;&#x51B2;&#x533A;)
*@param size &#x9876;&#x70B9;&#x7D22;&#x5F15;&#x6570;&#x636E;size
*@param data &#x9876;&#x70B9;&#x7D22;&#x5F15;&#x6570;&#x7EC4;
*@param usage &#x7F13;&#x51B2;&#x533A;&#x4F7F;&#x7528;&#x65B9;&#x5F0F;&#xFF0C;&#x5E38;&#x7528;GL_STATIC_DRAW(&#x4EC5;&#x4FEE;&#x6539;&#x4E00;&#x6B21;&#x540E;&#x91CD;&#x590D;&#x4F7F;&#x7528;)
*/</span>
<span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">glBufferData</span><span class="hljs-params">(GLenum   target, GLsizeiptr   size, <span class="hljs-keyword">const</span> <span class="hljs-keyword">void</span> * data, GLenum   usage)</span></span>;
</code></pre>
<h3 id="2-&#x5C06;shader&#x53D8;&#x91CF;&#x548C;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x5173;&#x8054;"><a name="2-&#x5C06;shader&#x53D8;&#x91CF;&#x548C;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x5173;&#x8054;" class="anchor-navigation-ex-anchor" href="#2-&#x5C06;shader&#x53D8;&#x91CF;&#x548C;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x5173;&#x8054;"><i class="fa fa-link" aria-hidden="true"></i></a>2. &#x5C06;Shader&#x53D8;&#x91CF;&#x548C;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x5173;&#x8054;</h3>
<p>&#x4E4B;&#x524D;&#x662F;&#x76F4;&#x63A5;&#x5C06;&#x9876;&#x70B9;&#x5C5E;&#x6027;&#x6570;&#x636E;&#x548C;Shader&#x53D8;&#x91CF;&#x8FDB;&#x884C;&#x5173;&#x8054;&#xFF0C;&#x4EE3;&#x7801;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-c++"><span class="hljs-comment">//&#x542F;&#x7528;&#x9876;&#x70B9;Shader&#x5C5E;&#x6027;(a_pos)&#xFF0C;&#x6307;&#x5B9A;&#x4E0E;&#x9876;&#x70B9;&#x5750;&#x6807;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x5173;&#x8054;</span>
glVertexAttribPointer(vpos_location, <span class="hljs-number">3</span>, GL_FLOAT, <span class="hljs-literal">false</span>, <span class="hljs-keyword">sizeof</span>(glm::vec3), kPositions);
<span class="hljs-comment">//&#x9876;&#x542F;&#x7528;&#x9876;&#x70B9;Shader&#x5C5E;&#x6027;(a_uv)&#xFF0C;&#x6307;&#x5B9A;&#x4E0E;&#x70B9;UV&#x6570;&#x636E;&#x8FDB;&#x884C;&#x5173;&#x8054;</span>
glVertexAttribPointer(a_uv_location, <span class="hljs-number">2</span>, GL_FLOAT, <span class="hljs-literal">false</span>, <span class="hljs-keyword">sizeof</span>(glm::vec2), kUvs);
</code></pre>
<p>&#x73B0;&#x5728;&#x6570;&#x636E;&#x90FD;&#x4E0A;&#x4F20;&#x5230;VBO&#x4E86;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x9700;&#x8981;&#x5C06;Shader&#x53D8;&#x91CF;&#x548C;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;&#x53E5;&#x67C4;&#x8FDB;&#x884C;&#x5173;&#x8054;&#xFF0C;&#x4EE3;&#x7801;&#x4FEE;&#x6539;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-c++"><span class="hljs-comment">//&#x6307;&#x5B9A;&#x5F53;&#x524D;&#x4F7F;&#x7528;&#x7684;VBO</span>
glBindBuffer(GL_ARRAY_BUFFER, kVBO);
<span class="hljs-comment">//&#x5C06;Shader&#x53D8;&#x91CF;(a_pos)&#x548C;&#x9876;&#x70B9;&#x5750;&#x6807;VBO&#x53E5;&#x67C4;&#x8FDB;&#x884C;&#x5173;&#x8054;&#xFF0C;&#x6700;&#x540E;&#x7684;0&#x8868;&#x793A;&#x6570;&#x636E;&#x504F;&#x79FB;&#x91CF;&#x3002;</span>
glVertexAttribPointer(vpos_location, <span class="hljs-number">3</span>, GL_FLOAT, <span class="hljs-literal">false</span>, <span class="hljs-keyword">sizeof</span>(Vertex), <span class="hljs-number">0</span>);
<span class="hljs-comment">//&#x542F;&#x7528;&#x9876;&#x70B9;Shader&#x5C5E;&#x6027;(a_color)&#xFF0C;&#x6307;&#x5B9A;&#x4E0E;&#x9876;&#x70B9;&#x989C;&#x8272;&#x6570;&#x636E;&#x8FDB;&#x884C;&#x5173;&#x8054;</span>
glVertexAttribPointer(vcol_location, <span class="hljs-number">4</span>, GL_FLOAT, <span class="hljs-literal">false</span>, <span class="hljs-keyword">sizeof</span>(Vertex), (<span class="hljs-keyword">void</span>*)(<span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">float</span>)*<span class="hljs-number">3</span>));
<span class="hljs-comment">//&#x5C06;Shader&#x53D8;&#x91CF;(a_uv)&#x548C;&#x9876;&#x70B9;UV&#x5750;&#x6807;VBO&#x53E5;&#x67C4;&#x8FDB;&#x884C;&#x5173;&#x8054;&#xFF0C;&#x6700;&#x540E;&#x7684;0&#x8868;&#x793A;&#x6570;&#x636E;&#x504F;&#x79FB;&#x91CF;&#x3002;</span>
glVertexAttribPointer(a_uv_location, <span class="hljs-number">2</span>, GL_FLOAT, <span class="hljs-literal">false</span>, <span class="hljs-keyword">sizeof</span>(Vertex), (<span class="hljs-keyword">void</span>*)(<span class="hljs-keyword">sizeof</span>(<span class="hljs-keyword">float</span>)*(<span class="hljs-number">3</span>+<span class="hljs-number">4</span>)));
</code></pre>
<p>&#x51FD;&#x6570;<code>glVertexAttribPointer</code> &#x6700;&#x540E;&#x7684;&#x53C2;&#x6570;&#x7531;&#x9876;&#x70B9;&#x5C5E;&#x6027;&#x6570;&#x7EC4;&#x53D8;&#x4E3A;&#x4E86;&#x6570;&#x636E;&#x504F;&#x79FB;&#x91CF;&#x3002;</p>
<p>&#x6570;&#x636E;&#x5173;&#x8054;&#x597D;&#x4E4B;&#x540E;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x8FDB;&#x884C;&#x7ED8;&#x5236;&#x4E86;&#x3002;</p>
<h3 id="3-&#x7ED8;&#x5236;"><a name="3-&#x7ED8;&#x5236;" class="anchor-navigation-ex-anchor" href="#3-&#x7ED8;&#x5236;"><i class="fa fa-link" aria-hidden="true"></i></a>3. &#x7ED8;&#x5236;</h3>
<p>&#x4E0A;&#x4E00;&#x8282;&#x4F7F;&#x7528;&#x9876;&#x70B9;&#x7D22;&#x5F15;&#x8FDB;&#x884C;&#x7ED8;&#x5236;&#x65F6;&#xFF0C;&#x9700;&#x8981;&#x5728;<code>glDrawElements</code>&#x4E2D;&#x4F20;&#x5165;&#x9876;&#x70B9;&#x7D22;&#x5F15;&#x6570;&#x636E;&#xFF0C;&#x6BCF;Draw&#x4E00;&#x6B21;&#x90FD;&#x4E0A;&#x4F20;&#x4E00;&#x6B21;&#x7D22;&#x5F15;&#x5230;GPU&#x3002;</p>
<pre><code class="lang-c++">glDrawElements(GL_TRIANGLES,<span class="hljs-number">36</span>,GL_UNSIGNED_SHORT,kIndices);<span class="hljs-comment">//&#x4F7F;&#x7528;&#x9876;&#x70B9;&#x7D22;&#x5F15;&#x8FDB;&#x884C;&#x7ED8;&#x5236;&#x3002;</span>
</code></pre>
<p>&#x73B0;&#x5728;&#x9876;&#x70B9;&#x7D22;&#x5F15;&#x6570;&#x636E;&#x90FD;&#x4E0A;&#x4F20;&#x5230;EBO&#x4E86;&#xFF0C;&#x5C31;&#x53EA;&#x9700;&#x8981;&#x6307;&#x5B9A;&#x5F53;&#x524D;&#x4F7F;&#x7528;&#x7684;EBO&#xFF0C;&#x7136;&#x540E;&#x76F4;&#x63A5;&#x7ED8;&#x5236;&#x3002;</p>
<pre><code class="lang-c++"><span class="hljs-comment">//&#x6307;&#x5B9A;&#x5F53;&#x524D;&#x4F7F;&#x7528;&#x7684;&#x9876;&#x70B9;&#x7D22;&#x5F15;&#x7F13;&#x51B2;&#x533A;&#x5BF9;&#x8C61;</span>
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, kEBO);
glDrawElements(GL_TRIANGLES,<span class="hljs-number">36</span>,GL_UNSIGNED_SHORT,<span class="hljs-number">0</span>);<span class="hljs-comment">//&#x4F7F;&#x7528;&#x9876;&#x70B9;&#x7D22;&#x5F15;&#x8FDB;&#x884C;&#x7ED8;&#x5236;&#xFF0C;&#x6700;&#x540E;&#x7684;0&#x8868;&#x793A;&#x6570;&#x636E;&#x504F;&#x79FB;&#x91CF;&#x3002;</span>
</code></pre>
<p>&#x7F16;&#x8BD1;&#x8FD0;&#x884C;&#xFF0C;&#x6548;&#x679C;&#x6B63;&#x786E;&#xFF1A;</p>
<p><a href="../../imgs/vbo_vertex_index/vbo/draw_with_vbo_ok.jpg" data-lightbox="b7659442-af80-4c0c-bf9c-7c714391dc38" data-title=""><img src="../../imgs/vbo_vertex_index/vbo/draw_with_vbo_ok.jpg" alt=""></a> </p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; captainchen all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">File Modify: 
2021-04-26 14:49:02
</span></footer></body></html>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="6.1 顶点索引.html" class="navigation navigation-prev " aria-label="Previous page: 6.1 顶点索引">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../7. Mesh和材质/7. Mesh和材质.html" class="navigation navigation-next " aria-label="Next page: 7. Mesh和材质">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"6.2 缓冲区对象","level":"2.7.2","depth":2,"next":{"title":"7. Mesh和材质","level":"2.8","depth":1,"path":"pages/7. Mesh和材质/7. Mesh和材质.md","ref":"pages/7. Mesh和材质/7. Mesh和材质.md","articles":[{"title":"7.1 导出Mesh文件","level":"2.8.1","depth":2,"path":"pages/7. Mesh和材质/7.1 导出Mesh文件.md","ref":"pages/7. Mesh和材质/7.1 导出Mesh文件.md","articles":[]},{"title":"7.2 使用Mesh文件","level":"2.8.2","depth":2,"path":"pages/7. Mesh和材质/7.2 使用Mesh文件.md","ref":"pages/7. Mesh和材质/7.2 使用Mesh文件.md","articles":[]},{"title":"7.3 Shader文件创建与使用","level":"2.8.3","depth":2,"path":"pages/7. Mesh和材质/7.3 Shader文件创建与使用.md","ref":"pages/7. Mesh和材质/7.3 Shader文件创建与使用.md","articles":[]}]},"previous":{"title":"6.1 顶点索引","level":"2.7.1","depth":2,"path":"pages/6. 索引与缓冲区对象/6.1 顶点索引.md","ref":"pages/6. 索引与缓冲区对象/6.1 顶点索引.md","articles":[]},"dir":"ltr"},"config":{"plugins":["expandable-chapters","tbfed-pagefooter","splitter","lightbox","anchor-navigation-ex","search-pro","livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy captainchen","modify_format":"YYYY-MM-DD HH:mm:ss"},"livereload":{},"splitter":{},"search-pro":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":false,"showLevel":false},"lightbox":{"jquery":true,"sameUuid":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"captainchen","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"游戏引擎浅入浅出","gitbook":"*"},"file":{"path":"pages/6. 索引与缓冲区对象/6.2 缓冲区对象.md","mtime":"2021-04-26T06:49:02.738Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-04-30T15:25:09.576Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lightbox/js/lightbox.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

